package com.yunchen.BooksManagementSystem.user.servlet;

import com.alibaba.fastjson.JSON;
import com.yunchen.BooksManagementSystem.user.dao.userDaoImpl;
import com.yunchen.BooksManagementSystem.user.entity.Books;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "GetAllBookServlet", value = "/GetAllBookServlet")
public class GetAllBookServlet extends HttpServlet {
    private userDaoImpl ud=new userDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        ArrayList<Books> booksArrayList=null;
        try {
            booksArrayList=ud.getAllBooks();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String jsonString= JSON.toJSONString(booksArrayList);
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
